//
//  LSHomeViewController.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/8/18.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSHomeViewController.h"
#import "LSHomeCell.h"
#import "LSHomeDataManager.h"
#import "LSFirstListenController.h"
#import "LSTestWrongQuesViewController.h"

@interface LSHomeViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
    BKBaseRequest *net;
}

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UIImageView *navView ;
@property (nonatomic,assign) CGFloat newy;
@property (nonatomic,assign) CGFloat oldy;

@end

@implementation LSHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _newy = 0;
    _oldy = 0;
    
    [self addBYItemviews:@[@"头像"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    
    //头部样式背景
    UIImageView *navView =  [[UIImageView alloc] init];
    self.navView = navView;
    navView.image = [UIImage imageNamed:@"蓝色背景"];
    [self.view addSubview:navView];
    navView.backgroundColor = [UIColor blueColor];
    [navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(190);
    }];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT(13);
    NSString *titleText = @"中考英语听说学习训练平台\n错题强化训练";
    NSRange range = [titleText rangeOfString:@"错题强化训练"];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:titleText];
    [attr addAttributes:@{NSFontAttributeName:FONT(19),NSForegroundColorAttributeName:[UIColor whiteColor]} range:range];
    titleLabel.attributedText = attr;
    [navView addSubview:titleLabel];
    CGFloat top = 20;
    if (isPhoneX) {
        top = 44;
    }
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navView).offset(top);
        make.centerX.equalTo(navView);
        make.size.mas_equalTo(CGSizeMake(160, 40));
    }];
    
    //白色圆角背景
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    [navView addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(navView);
        make.centerX.equalTo(navView);
        make.size.mas_equalTo(CGSizeMake(kWidth, 40));
    }];
    [backView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(20, 20) viewRect:CGRectMake(0, 0, kWidth, 40)];
    
    //提示信息
    UILabel *infoLabel = [[UILabel alloc] init];
    [navView addSubview:infoLabel];
    infoLabel.text = @"正在诊断您的听说薄弱点，并智能推荐听说训练方式!";
    infoLabel.textColor = [UIColor colorWithHex:0x666666];
    infoLabel.font = FONT(13);
    CGSize size = [infoLabel.text sizeWithAttributes:@{NSFontAttributeName:FONT(13)}];
    [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(navView).offset(-10);
        make.centerX.equalTo(navView);
        make.size.mas_equalTo(CGSizeMake(size.width+1, 13));
    }];
    UIButton *tipBtn = [[UIButton alloc] init];
    [navView addSubview:tipBtn];
    [tipBtn setImage:[UIImage imageNamed:@"说明"] forState:UIControlStateNormal];
    [tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(infoLabel.mas_right).offset(2);
        make.centerY.equalTo(infoLabel);
        make.size.mas_equalTo(CGSizeMake(13, 13));
    }];
    
    //图表
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[LSHomeCell class] forCellReuseIdentifier:@"LSHomeCell"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            make.top.equalTo(navView.mas_bottom);
        } else {
            make.top.equalTo(navView.mas_bottom);
            make.bottom.equalTo(self.view);
        }
    }];
   // self.tableView.tableHeaderView = [self createTableViewHeader];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self loadData];
    
}


//- (UIView*)createTableViewHeader {
//
//    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
//    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, 40)];
//    titleLabel.text = @"正在诊断您的听说薄弱点，并智能推荐听说训练方式";
//    titleLabel.font = [UIFont systemFontOfSize:14.0f];
//    [header addSubview:titleLabel];
//
//
//    return header;
//}

- (void)loadData {
    
    LSHomeDataManager *manager =  [[LSHomeDataManager alloc] init];
    NSDictionary *dict = @{@"SchoolID":KGetUserDefaults(@"SchoolID"),
                             @"XH":DR_UserInfo.UserID,
                             @"HTestID":KGetUserDefaults(@"TestLayer"),
                             @"SubjectID":@"C"
                             };
    manager.requestParam = @{@"DictString":[NSObject objectToJson:dict]};
    self.dataArray = [NSMutableArray array];
    
    double delayInSeconds = 3.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //执行事件
        WeakObj(self);
        [manager requestWithCompltionSuccess:^(ModelHome *model) {
            [selfWeak.dataArray addObjectsFromArray:model.InfoList.firstObject.WeakList];
            [selfWeak.tableView reloadData];
        } failuer:^(NSString *error) {
            
        }];
    });
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    // 设置导航栏的默认属性(默认为不透明、黑色状态栏)
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
}

#pragma mark - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    net = [BKBaseRequest new];
//    [net gr_addRequestForListDirectoryAtPath:@"quesbank/ZJ/OralQuesBank" success:^(NSString *unzipPath, NSArray *subPaths) {
//        for (NSString *path in subPaths) {
//            if ([path hasSuffix:@"xml"]) {
//
//                NSDictionary *dic = [[XMLDictionaryParser sharedInstance] dictionaryWithFile:path];
//                NSLog(@"sss:%@", dic);
//            }
//        }
//    } failure:nil];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ScalesH * 260;
}

#pragma mark - tableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LSHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSHomeCell"];
    WeakObj(self);
    cell.pushBlock = ^{
        if (indexPath.row==0) {
//            LSFirstListenController *vc = [[LSFirstListenController alloc] init];
//            [selfWeak push:vc];
            
            LSTestWrongQuesViewController *vc =[[LSTestWrongQuesViewController alloc] init];
            [selfWeak push:vc];
            
        }
    };
    if (self.dataArray.count>0) {
        weak *modelweak = self.dataArray[indexPath.row];
       // modelweak.ZDTimeString = @"";
        cell.weakModel = modelweak;
    }
    
    return cell;
}

#pragma mark - UISrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    BOOL scrollUporDown = false;
    if ([scrollView isEqual:self.tableView]) {
        NSLog(@"contentOffset=%f",scrollView.contentOffset.y);
        _newy = scrollView.contentOffset.y;
        
        if(_newy>_oldy) {
            scrollUporDown = YES;//朝上
        } else {
            scrollUporDown = NO;//朝下
        }
        _oldy = _newy;
        
        if (scrollUporDown==YES) {
            if (_newy>0) {
                [self.navView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(kNavgationBar+40);
                }];
                //添加更新约束动画
                [UIView animateWithDuration:0.35 animations:^{
                    [self.view layoutIfNeeded];
                }];
            }
           
        } else {
            if (_newy<0) {
                [self.navView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(190);
                }];
                //添加更新约束动画
                [UIView animateWithDuration:0.35 animations:^{
                    [self.view layoutIfNeeded];
                }];
            }
        }

        
    }
    
}

@end
